package com.micro.frog.rbac.controller;

import com.micro.frog.common.base.Result;
import com.micro.frog.rbac.dto.RoleMenuDTO;
import com.micro.frog.rbac.dto.UserRoleDTO;
import com.micro.frog.rbac.service.RoleMenuService;
import com.micro.frog.rbac.service.UserRoleService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 权限管理
 *
 * @author :  ljh
 * @date : 2018/4/12
 **/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "/rbac")
public class RbacController {

    private final UserRoleService userRoleService;
    private final RoleMenuService roleMenuService;

    /**
     * 查询关联角色和用户
     */
    @GetMapping(value = "/roleUser/query")
    public Result queryUserRole(Long roleId) {
        return Result.success(userRoleService.queryUserByRoleId(roleId));
    }

    /**
     * 查询关联角色和用户
     */
    @GetMapping(value = "/roleMenu/query")
    public Result queryRoleMenu(Long roleId) {
        return Result.success(roleMenuService.queryMenuIdsByRoleId(roleId));
    }

    /**
     * 更新关联角色和用户
     */
    @PostMapping(value = "/roleUser/update")
    public Result updateUserRole(@RequestBody UserRoleDTO req) {
        if (req == null || null == req.getRoleId()) {
            return Result.error("参数不能为空");
        }
        userRoleService.updateRoleUsers(req.getRoleId(), req.getUserIds());
        return Result.success();
    }

    /**
     * 更新关联角色和菜单
     */
    @PostMapping(value = "/roleMenu/update")
    public Result updateRoleMenu(RoleMenuDTO req) {
        if (req == null || null == req.getRoleId()) {
            return Result.error("参数不能为空");
        }
        roleMenuService.updateRoleMenus(req.getRoleId(), req.getMenuIds());
        return Result.success();
    }

}
